package com.xdtech.tms.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.xdtech.common.utils.ApplicationContextUtil;
import com.xdtech.common.utils.DateUtil;
import com.xdtech.common.utils.DateUtil.DateStyle;
import com.xdtech.tms.vo.DispathOrderInfo;
import com.xdtech.tms.vo.DriverInfo;
import com.xdtech.web.controller.BaseController;
import com.xdtech.web.model.ResultMessage;

@Controller
public class TmsController extends BaseController{
	@RequestMapping(value="/tmsLogin.action")
	@ResponseBody
	public ResultMessage tmsLogin(String telphone,String password) {
		ResultMessage r = new ResultMessage();
//		if ("123456789".equals(telphone)&&"123456".equals(password)) {
			r.setObj(new DriverInfo(telphone, password, "jovi"));
//		}else {
//			r.setFailMsg("电话号码或密码有误");
//		}
		log.info("登录信息="+telphone+","+password);
		return r;
	}
	
	@RequestMapping(value="/tmsUpdatePwd.action")
	@ResponseBody
	public ResultMessage tmsUpdatePwd(String oldPwd,String newPwd,String newPwd2) {
		ResultMessage r = new ResultMessage();
		if (!"123456".equals(oldPwd)) {
			r.setFailMsg("原密码输入错误");
		}else {
			if (!newPwd.equals(newPwd2)) {
				r.setFailMsg("新密码两次输入不一致");
			} else {
				
			}
		}
		return r;
	}
	
	@RequestMapping(value="/tmsLoadDisList.action")
	@ResponseBody
	public ResultMessage tmsLoadDisList(String statu,int pageNo,int pageSize) {
		ResultMessage r = new ResultMessage();
		List<DispathOrderInfo> orderInfos = new ArrayList<DispathOrderInfo>();
		int beginIndex = (pageNo-1)*pageSize+1;
		int endIndex = beginIndex+pageSize;
		
		for (int i = beginIndex; i < endIndex; i++) {
			orderInfos.add(new DispathOrderInfo(Long.valueOf(i+""), "DO"+i, "计划30吨", DateUtil.getDate(new Date()), DateUtil.getDate(new Date()), "厦门软件园二期", "泉州南安XXXX", "需要清灌",statu));
		}
		r.setObj(orderInfos);
		return r;
	}
	
	@RequestMapping(value="/tmsLoadDisInfo.action")
	@ResponseBody
	public ResultMessage tmsLoadDisInfo(Long id) {
		ResultMessage r = new ResultMessage();
		
		r.setObj(new DispathOrderInfo(id, "DO"+id, "计划30吨", DateUtil.getDate(new Date()), DateUtil.getDate(new Date()), "厦门软件园二期", "泉州南安XXXX", "需要清灌","1"));

		return r;
	}
	
	@RequestMapping(value="/tmsDisExecute.action")
	@ResponseBody
	public ResultMessage tmsDisExecute(Long id,String doNo,String actionType) {
		ResultMessage r = new ResultMessage();
		//执行派车单状态
		log.info("派车单状态执行变更"+doNo+"，actionType="+actionType);
		return r;
	}
	@ResponseBody
	@RequestMapping(value = "/tmsUploadFile.action")
	public ResultMessage uploadFile(HttpServletRequest request,
			@RequestParam("filedata") MultipartFile file){
		List<String> filePathList = new ArrayList<String>();
		ResultMessage rs = new ResultMessage();
		try {
			String rootUrl = ApplicationContextUtil.getWebappUrl();
			String filePath = "uploadFile/"+DateUtil.dateToString(new Date(), DateStyle.YYYYMMDD);
			String uploadDir = request.getRealPath("/"+filePath);
			File dirPath = new File(uploadDir);
			if (!dirPath.exists()) {
				dirPath.mkdirs();
			}
			int indexFile = file.getOriginalFilename().lastIndexOf(".");
			String fileName = System.currentTimeMillis()+"_"+RandomStringUtils.random(4, "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")+file.getOriginalFilename().substring(indexFile, file.getOriginalFilename().length());
//			System.out.println(file.getContentType());
			String imgUrl = filePath+"/"+fileName;
			filePath = uploadDir + "/"+fileName;
			file.transferTo(new File(filePath));
//			filePathList.add(imgUrl);
			rs.setObj(imgUrl);
			
		} catch (Exception e) {
			e.printStackTrace();
			rs.setFailMsg("上传失败");
		}
		return rs;
	}
	
	@ResponseBody
	@RequestMapping(value = "/tmsUploadFiles.action")
    public ResultMessage filesUpload(HttpServletRequest request,@RequestParam("attachments") MultipartFile[] files) {  
        //判断file数组不能为空并且长度大于0  
		System.out.println(request.getParameter("doId"));
		System.out.println(request.getParameter("doId"));
		System.out.println(request.getParameter("doId"));
		System.out.println(request.getParameter("doId"));
		System.out.println(request.getParameter("doId"));
        if(files!=null&&files.length>0){  
            //循环获取file数组中得文件  
            for(int i = 0;i<files.length;i++){  
                MultipartFile file = files[i];  
                //保存文件  
                uploadFile(request,file);  
            }  
        }  
        // 重定向  
        return new ResultMessage();  
    }  

}


